/**   
 * @title ExcelUtil.java 
 * @package com.hopechart.importutil.util 
 * @description Excel工具包 
 * @author daijiang@hopechart.com
 * @date 2016年1月13日 上午10:36:27 
 * @version v1.0.0
 * @copyright 杭州鸿泉数字设备有限公司
 */
package com.hopechart.importutil.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jplus.hyberbin.excel.service.ImportTableService;

import com.hopechart.importutil.exception.BusinessException;

/**
 * @className ExcelUtil
 * @description Excel工具包 
 * @author daijiang@hopechart.com
 * @date 2016年1月13日 上午10:36:27
 * 
 */
public class ExcelUtil {

    private static final String _2003_EXCEL = "xls";
    private static final String _2007_EXCEL = "xlsx";
    
    private static final Integer DEFAULT_SHEET_INDEX = 0;
    
    private static final Integer START_ROW_NUM = 1;

    public static HSSFWorkbook getWorkbook(InputStream inputStream) {
        try {
            return new HSSFWorkbook(inputStream);
        } catch (IOException e) {
            throw new BusinessException("创建HSSFWorkbook", e);
        }
    }
    
    public static XSSFWorkbook getXSSFWorkbook(InputStream inputStream) {
        try {
            return new XSSFWorkbook(inputStream);
        } catch (IOException e) {
            throw new BusinessException("创建XSSFWorkbook", e);
        }  
    }

    public static Sheet getSheet(HSSFWorkbook workbook) {
        if (null == workbook) {
            throw new BusinessException("HSSFWorkbook对象为空！");
        }
        Sheet sheetAt = workbook.getSheetAt(DEFAULT_SHEET_INDEX);
        if (null == sheetAt) {
            throw new BusinessException("获取Sheet数据异常！");
        }
        return sheetAt;
    }
    
    public static Sheet getSheet(XSSFWorkbook workbook) {
        if (null == workbook) {
            throw new BusinessException("HSSFWorkbook对象为空！");
        }
        Sheet sheetAt = workbook.getSheetAt(DEFAULT_SHEET_INDEX);
        if (null == sheetAt) {
            throw new BusinessException("获取Sheet数据异常！");
        }
        return sheetAt;
    }

    public static <T> List<T> importExcelData(Class<T> clazz, InputStream inputStream, 
            String[] cols, String fileType) {
        try {
            Sheet sheetAt = null;
            if(_2003_EXCEL.equals(fileType)) {
                sheetAt = getSheet(getWorkbook(inputStream));
            } else if(_2007_EXCEL.equals(fileType)) {
                sheetAt = getSheet(getXSSFWorkbook(inputStream));
            } else {
                return new ArrayList<T>();
            }
            ImportTableService service = new ImportTableService(sheetAt);
            service.setStartRow(START_ROW_NUM);
            service.doImport();
            List<T> datas = service.read(cols, clazz);
            return datas;
        } catch (Exception e) {
            throw new BusinessException("Sheet转对象实体时异常！", e);
        }
    }
}
